package com.night.card.controller;


import com.night.card.service.interfs.CardService;
import com.night.card.until.EmailUntil;
import com.night.common.entity.card.Card;
import com.night.common.entity.card.CardIn;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @ClassName: RabbitMqInMoneyCop
 * @Description: 消息队列充值
 * @author: CHX42
 * @date: 2022/10/28-11:54
 * @version: v1.0
 */
@Component
@Slf4j
public class RabbitMqInMoneyCop {
    @Resource
    private CardService cardService;

    @RabbitListener(queues = "inMoneyOrder")
    public void inMoney(CardIn cardIn, Message message, Channel channel) throws IOException {
        try{
            cardService.inMoney(cardIn);
            throw new RuntimeException("模拟异常，往死信发消息");
        }catch (Exception exception){
            exception.printStackTrace();
            //消息补发到死信
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        }finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }


    }

    @RabbitListener(queues = "inMoneyOrderDlx")
    public void falseInMoney(CardIn cardIn, Message message, Channel channel) throws IOException {
        Card card = cardService.getById(cardIn.getCard().getId());
        cardIn.setCard(card);
        EmailUntil.sendMail(cardIn);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


}
